//回单管理详情（零担）

interface IReceiptManagentBreakViewModel extends ng.IScope{
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 承运单详情
     */
    carrierOrderData:invoiceManagement;
    /**
     * 订单列表
     */
    orderList:Array<invoiceManagementOrder>;
    /**
     * 油卡状态下拉
     */
    oilCardStatusDropDown:ValueListItemResponse[];
    /**
     * 合同状态下拉
     */ 
    contractStatusDropDown:ValueListItemResponse[];
    /**
     * 合同信息
     */
    contractData:invoiceManagementContract;
    /**
     * 油卡状态
     */
    oilCardStatus:string;
    /**
     * 合同状态
     */
    contractStatus:string;
    /**
     * 费用信息列表
     */
    orderCostList:Array<invoiceManagementFee>;
    /**
     * 查费用详情
     */
    orderCost:(item:invoiceManagementFee)=>void;
    /**
     * 付款信息查看详情的数据
     */
    orderCoseData:invoiceManagementFee;

    /**
     * 是否可修改备注信息(false为可修改)
     */
    isCanUpdateRemarks:boolean;
    /**
     * 订单数据
     */
    orderData:invoiceManagementOrder;
    /**
     * 总线路详情数据
     */
    orderTotalData:invoiceManagementOrder;
    /**
     * 子线路数据
     */
    orderChildData:Array<invoiceManagementOrderChild>;
    /**
     * 子线路查看详情数据
     */
    orderChildDataDetail:invoiceManagementOrderChild;
    /**
     * 回单状态
     */
    receiptStatus:string;
    /**
     * 回单时间
     */
    receiptTime:string;
    /**
     * 回单状态下拉
     */
    receiptStatusDropDown:ValueListItemResponse[];
    /**
     * 回单附件
     */
    receiptInfoListArr:Array<any>;
    /**
     * 查看订单详情事件
     */
    orderDetail:(item:invoiceManagementOrder) => void;
    /**
     * 确定事件(修改合同状态)
     */
    changeContractStatus: (id:string) => void;
    /**
     * 取消事件
     */
    backUrl:(id:string) => void;
    /**
     * 总线路查看详情事件
     */
    orderDataDetail:(row:invoiceManagementOrder) =>void;
    /**
     * 子线路查看详情事件
     */
    orderChildDetail:(item:invoiceManagementOrderChild)=>void;
    /**
     * 详情确定事件(修改回单)
     */
    saveReceipts: (id:string) => void;
    /**
     * 详情取消事件
     */
    back:(id:string) => void;
    /**
     * 预付款1
     */
    advancePriceOne:string;
    /**
     * 预付款方式1
     */
    advancePriceTypeOne:string;
    /**
     * 预付款2
     */
    advancePriceTwo:string;
    /**
     * 预付款方式2
     */
    advancePriceTypeTwo:string;



    //11.1新增
    /**
     * 结算单据状态
     */
    receiptBillStatus:string;
    /**
     * 结算单据状态下拉
     */
    receiptBillStatusDropDown:ValueListItemResponse[];

     /**
     * 点击图片时候触发，用以旋转放大图片
     */
    handleImgClick:(item:any)=>void;
    /**
     * 用以旋转放大的图片的路径
     */
    selectedImgSrc:string;
}


class ReceiptManagentBreakController{
    /**
     * 承运单id
     */
    id:string;

    /**
     * 回单附件地址
     */
    baseImageUrl: string;
    //附件
    uploaderList: ReceiptAttachment[]=[];
    imgList = [];
    constructor(private $scope:IReceiptManagentBreakViewModel,private feeService:IFeeService,private valueService:IValueService,
        private orderService:IorderService ,private $location: ng.ILocationService,private routeService: routeService,
        private $ngBootbox: BootboxService,private $http: ng.IHttpService,
        private $state: angular.ui.IStateService,
        private commonService:ICommonService,private ossUploaderService:ossUploaderService){
        this.init();
        this.baseImageUrl = this.routeService.getBaseUrl() + "attachment/";
    }
    
    init():void{
        this.id = this.$location.search().id;

        this.$scope.isCanUpdateRemarks = false;
       
        this.loadStatusDropDown();

        this.$scope.orderDetail = this.orderDetail;
        this.$scope.changeContractStatus = this.changeContractStatus;
        this.$scope.backUrl = this.backUrl;

        $('#openreceiptTime').click(() => {
		    $('#receiptTime').datetimepicker('show');
		});

        this.loadCarrierData();
        this.$scope.orderCost = this.orderCost;

        //默认结算单据状态是  无结算单据
        this.$scope.receiptBillStatus = "1";
        this.$scope.handleImgClick=(item)=>{this.$scope.selectedImgSrc=item.imgSrc;}
    }

    /**
     * 获取付款信息详情
     */
    orderCost=(item:invoiceManagementFee)=>{
        this.$scope.orderCoseData = item;
    }

    /**
     * 获取承运单详情
     */
    loadCarrierData = (isSaveReceipt?:boolean) =>{
        this.orderService.invoiceManagementDetail(this.id).then((res)=>{
            this.$scope.carrierOrderData = res.data;
            this.$scope.orderList = res.data.orderList;
            /**
             * 预付款
             */
            if(res.data.advancePaymentList.length>0 && res.data.advancePaymentList.length<=1){
                this.$scope.advancePriceOne = res.data.advancePaymentList[0].advancePrice;
                this.$scope.advancePriceTypeOne = res.data.advancePaymentList[0].advancePriceTypeStr;
                this.$scope.advancePriceTwo = "";
                this.$scope.advancePriceTypeTwo = "";
            }
            if(res.data.advancePaymentList.length>1){
                this.$scope.advancePriceOne = res.data.advancePaymentList[0].advancePrice;
                this.$scope.advancePriceTypeOne = res.data.advancePaymentList[0].advancePriceTypeStr;
                this.$scope.advancePriceTwo = res.data.advancePaymentList[1].advancePrice;
                this.$scope.advancePriceTypeTwo = res.data.advancePaymentList[1].advancePriceTypeStr;
            }
            /**
             * 合同信息
             */
            this.$scope.contractData = res.data.contract;
            this.$scope.oilCardStatus = res.data.contract.oilCardStatusEnum;
            /**
             * 合同详情接口
             */
            this.feeService.contractDetail(this.$scope.contractData.id).then((result) => {  
                this.$scope.contractStatus = result.data.contractStatus;   
            });
            this.$scope.orderCostList = res.data.feeList;
        });       
    }

    /**
     * 状态下拉(油卡状态,合同状态)
     */
    loadStatusDropDown=()=>{
        this.$scope.contractStatusDropDown = this.valueService.getContractStatusList().data.slice(1,3);
        this.$scope.oilCardStatusDropDown = this.valueService.getOilCCardStatusList().data;
    }

    /**
     * 查看订单详情事件
     */
    orderDetail = (item:invoiceManagementOrder) => {
        /**
         *获取订单数据(总单数据)
         */
        this.$scope.orderData = item ;
        /**
         * 回单状态
         */
        this.$scope.receiptStatus = item.receiptStatusEnum;

        /**
         * 回单签收日期
         */
        this.$scope.receiptTime = item.receiptTime;
        /**
         *子线路数据
         */
        this.$scope.orderChildData = item.childList;
        /**
         * 回单状态下拉
         */
        this.loadReceiptStatusDropDown();


        //11.1 新增
        /**
         * 结算单据状态
         */
        if(item.settlementStatusEnum=="0"){
            this.$scope.receiptBillStatus="1";
        }else{
            this.$scope.receiptBillStatus = item.settlementStatusEnum;
        }
         

        /**
         * 结算单据状态下拉的
         */
        this.loadReceiptBillStatusDropDown();

        /**
         * 总线路查看详情事件
         */
        this.$scope.orderDataDetail = (() =>{
            this.$scope.orderTotalData = item;
        });
        /**
         * 子线路查看详情事件
         */
        this.$scope.orderChildDetail = this.showOrderChildDetail;
        /**
         * 详情确定事件(修改回单)
         */
        this.$scope.saveReceipts = this.saveReceipts;
        /**
         * 详情取消事件
         */
        this.$scope.back = this.back;   
        this.imgList = [];
        //回单附件
        this.getReceiptAttchment(item.childList);
        
    }

    /**
     * 回单附件
     */
    getReceiptAttchment(data:invoiceManagementOrderChild[]):void{
        this.uploaderList = [];
        this.$scope.receiptInfoListArr = [];
        let loadC = [];
        const OssFile = this.ossUploaderService.getOssFile();


        data.forEach((item,index)=>{
            this.$scope.receiptInfoListArr.push({
                title:'回单'+(index+1),
                uploader:null,
                uploaderName:'receipt'+(index+1)+'Uploader'
            })

            this.$scope.receiptInfoListArr[index].uploader=new  OssFile(this.$scope,this.$scope.receiptInfoListArr[index].uploader,{uploaderList:this.uploaderList,tag:index+1,multiple:true});
            this.$scope.receiptInfoListArr[index].uploader.onSuccess=(fileItem=>{
                this.onSuccessItem(item.id,fileItem.fileName, fileItem.imgSrc,fileItem._file);
                    if (this.$scope.receiptTime == "" || this.$scope.receiptTime == null || this.$scope.receiptTime == "-") {
                        this.$scope.receiptTime = String(this.commonService.transformTime(new Date(), "yyyy-MM-dd HH:mm:ss"));
                    }
            })
            
            item.receiptAttachments.forEach((itemR,indexR)=>{
                this.uploaderList.push({
                    orderChildId:item.id,
                    name:itemR.attachmentName,
                    path:itemR.attachmentPath,
                    creationTime:itemR.attachmentCreationTime
                });
                // this.commonService.showFileItem(itemR, this.$scope[childName]);
                this.$scope.receiptInfoListArr[index].uploader.addByAttach(itemR)
            });
            
        });
    }



    /**
     * 选择上传文件后
     */
    onSuccessItem = (childerId:string,fileName: string, filePath: string,file?:any) => {
        file.fileId = filePath;
        let attachment = {
            orderChildId: childerId,
            name: fileName,
            path: filePath,
            creationTime: ''
        };
        this.uploaderList.push(attachment);
    }

    /**
     * 回单状态下拉
     */
    loadReceiptStatusDropDown():void{
        this.$scope.receiptStatusDropDown = this.valueService.getReceiptStatusList().data;
    }

    /**
     * 结算单据状态下拉
     */
    loadReceiptBillStatusDropDown():void{
        this.$scope.receiptBillStatusDropDown = this.valueService.getReceiptBillStatusDropDown().data;
    }



    /**
     * 子线路查看详情事件
     */
    showOrderChildDetail = (item:invoiceManagementOrderChild) => {
        this.$scope.orderChildDataDetail = item;
    }

    /**
     * 确定事件(修改合同状态)
     */
    changeContractStatus = (id:string) =>{
        this.$ngBootbox.confirm('确定修改合同状态吗？').then(() =>{
            if (!this.$scope.orderList.every(item=>{
                return Boolean(item.receiptTime)
                })) {
				this.$ngBootbox.alert("请填写回单时间");
                return;
            }
            this.feeService.carrierOrderRemarks(
                this.id,
                this.$scope.carrierOrderData.remarks
            ).then((res) =>{},(rej) =>{});
            this.feeService.changeContractStatus(
                this.$scope.contractData.id,
                this.$scope.oilCardStatus,
                this.$scope.contractStatus
            ).then((res) =>{
                // this.$state.go("app.fee.receiptManagentList");
            },(rej)=>{});
        },()=>{ return;});
    }
    
    /**
     * 取消事件
     */
    backUrl = () =>{
        this.$state.go("app.fee.receiptManagentList", { name:'receiptManagent' });
    }

    /**
     * 详情确定事件(修改回单)
     */
    saveReceipts = (id:string) =>{
        if (this.$scope.receiptTime == "" || this.$scope.receiptTime == null || this.$scope.receiptTime == "-") {
            if (this.$scope.receiptStatus != "" && this.$scope.receiptStatus != "1" && this.$scope.receiptStatus != null) {
				this.$ngBootbox.alert("请填写回单时间");
                return;
            }
        }
        if( this.$scope.receiptBillStatus==null || this.$scope.receiptBillStatus=="" || this.$scope.receiptBillStatus=="0"){
            this.$ngBootbox.alert("请选择结算单据状态");
            return;
        }
		this.$ngBootbox.confirm('确定修改回单和结算单据状态吗?').then(() => {
            this.feeService.receipts(
                false,
                this.$scope.orderData.id,
                this.$scope.receiptStatus,
                this.$scope.receiptTime,
                this.uploaderList,
                this.$scope.receiptBillStatus
                ).then((res) => {
                    $('#detail').modal('hide');
                    this.loadCarrierData(true);
                },(rej) =>  {  });
        },() => { return; });
    }

    /**
     * 详情取消事件
     */
    back = (id:string) => {
        this.$state.go("app.fee.receiptManagentBulk", { id: this.$scope.orderData.id });
    }
}

angular.module("tms").controller("receiptManagentBreakBulkController", ["$scope", "feeService", "valueService",
     "orderService", "$location",  "routeService", "$ngBootbox" , 
     "$http", "$state","commonService","ossUploaderService", ReceiptManagentBreakController]);